<?php

namespace app\index\controller;

use think\Db;
use think\Exception;

class Notes extends IndexController
{
    /**
     * 通过Ajax显示列表
     *
     * @return \think\Response
     */
    public function ajaxChange()
    {
        $uniteid = input('post.')['uniteid'];
        $cid = input('post.')['cid'];
        // 查询数据库
        if( empty($cid) ){
            // 查询所有笔记
            $data = Db::name('notes')
                ->field('id, uniteid, updatetime, content, unitename')
                ->where('uid', session('web')['uid'])
                ->where('uniteid', $uniteid)
                ->select();
            return  $data;
        } else {
            // 查询一课笔记
            $data = Db::name('notes')
                ->field('id, uniteid, updatetime, content, unitename')
                ->where('uid', session('web')['uid'])
                ->where('cid', $cid)
                ->select();
            return  $data;
        }



    }

    /**
     * 显示课程笔记
     *
     */
    public function classNote ()
    {
        $uniteid = input('post.')['class'];
        $cid = input('post.')['cid'];


        // 查询拥有单元
        $unite = Db::name('courseunite')
            ->field('name, id, pid')
            ->where('courseid', $cid)
            ->select();
        $tree = $this->getTree($unite);


        // 查询笔记
        $data = Db::name('notes')
            ->field('id, uniteid, mktime, content, unitename')
            ->where('uid', session('web')['uid'])
            ->where('cid', $cid)
            ->where('uniteid', $uniteid)
            ->select();

        return view('note/noteslist', [
            'tree' => $tree,
            'data' => $data,

        ]);

    }
    /**
     * 显示创建资源表弹框.
     *
     * @return \think\Response
     */
    public function create()
    {
        $cid = input('post.')['id'];
        // 查询拥有单元
        $unite = Db::name('courseunite')
            ->field('name, id, pid')
            ->where('courseid', $cid)
            ->select();
        $tree = $this->getTree($unite);
        return $tree;
    }

    /**
     * 保存新建的资源
     * @return \think\Response
     */
    public function save()
    {
        $data = input('post.');



        // 开启事务
        Db::startTrans();
        try{
            // 添加数据表
            $res = Db::name('notes')
                ->insertGetId($data);

            if ( $res>0 ){
                Db::commit();
                return 200;
            }

        }catch (Exception $exception){
            Db::rollback();
            return 404;

        }

    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        // 查询拥有单元
        $unite = Db::name('courseunite')
            ->field('name, id, pid')
            ->where('courseid', $id)
            ->select();
        $tree = $this->getTree($unite);


        // 查询所有笔记
        $data = Db::name('notes')
            ->field('id, uniteid, updatetime, content, unitename')
            ->where('uid', session('web')['uid'])
            ->where('uniteid', $id)
            ->paginate(5);

        // 查询课程名
        $cname = Db::name('course')
            ->field('name')
            ->where('id', $id)
            ->find();

        $name = $cname['name'];

        $page = $data->render();
        $this->assign('cid', $id);
        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->assign('tree', $tree);
        $this->assign('cname',$name);

        return $this->fetch('note/noteslist');



    }

    public function getTree (&$list, $parentId = 0, $level = 0, $nbsp = '-- ')
    {
        static $tree = array();
        foreach ($list as $v) {
            if ($v['pid'] == $parentId) {
                //说明找到，保存
                $v['nbsp'] = str_repeat($nbsp, $level);
                $v['level'] = $level;
                $tree[] = $v;
                //继续找
                $this->getTree($list, $v['id'], $level + 1);
            }
        }
        return $tree;
    }


    /**
     * 保存更新的资源
     *
     */
    public function update()
    {
        $nid = input('post.')['nid'];
        $data= input('post.')['content'];

        $data = strip_tags($data);

        // 开启事务
        Db::startTrans();
        try{

            $res = Db::name('notes')
                ->where('id', $nid)
                ->setField('content',$data);

            if($res>0){
                Db::commit();
                return 200;
            }

        }catch(Exception $exception){
            Db::rollback();
            return 404;
        }

    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        $nid = input('post.')['nid'];
        // 开启事务
        Db::startTrans();
        try{
            $res = Db::name('notes')
                ->where('id', $nid)
                ->delete();
            if($res>0){
                Db::commit();
                return 200;
            }
        }catch(Exception $exception){
            Db::rollback();
            return 404;
        }
    }
}



